<?php

namespace app\admin\controller;

use app\api\model\ConfigPersonal;
use app\api\model\Privatechat;
use think\Db;
use think\Session;
use app\api\model\Task;
use app\api\model\ConfigSystem as Config;

class Withdraw extends Common
{
    public function lists()
    {
        $data = Db::name('withdraw_style')->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }

    // 添加/编辑提现选项
    public function addLists()
    {

        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');
            $config = Config::index('', 'rate');
            if($post['money'] * $config['rate'] < $post['price']){
                return ['err' => '金币比例过高'];
            }

            if (!$id) {
                $db = Db::name('withdraw_style')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('withdraw_style')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('withdraw_style')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    // 删除提现选项
    public function delLists()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('withdraw_style')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}